﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WebkitIISHost.ReadCard;

namespace WebkitIISHost
{
    public partial class frmReadCardSetting : Form
    {
        private WorKer worker;
        public frmReadCardSetting()
        {
            InitializeComponent();
        }

        private void frmReadCardSetting_Load(object sender, EventArgs e)
        {
            cboSDCard.SelectedIndex = 0;
        }

        private void Start_Click(object sender, EventArgs e)
        {
            if (worker == null || !worker.backgroundWorker.IsBusy)
            {
                progressBar1.Visible = this.lbl.Visible = true;

                worker = new WorKer();
                worker.SetText += new WorKer.SetTextEventHandler(worker_SetText);
                worker.Progress += new WorKer.ProgressEventHandler(worker_Progress);

                worker.Drive_dir = (this.cboSDCard.SelectedIndex + 2).ToString();
                worker.Drive_Name = this.cboSDCard.Text.ToString();
                worker.IsDataToText = "1";

                worker.Start();
            }
        }

        private void Suspend_Click(object sender, EventArgs e)
        {
            worker.Suspend();
        }

        private void Continue_Click(object sender, EventArgs e)
        {
            worker.Continue();
        }

        private void Stop_Click(object sender, EventArgs e)
        {
            worker.Stop();
            progressBar1.Value = 0;
            lbl.Text = "";

            progressBar1.Visible = lbl.Visible = false;
        }

        void worker_SetText(object sender, CustomEventArgs e)
        {
            SetText(e);
        }

        void worker_Progress(object sender, CustomEventArgs e)
        {
            SetProgress(e);
        }

        private void SetText(object i)
        {
            if (this.lbl.InvokeRequired)
            {
                SetTextCallback d = new SetTextCallback(SetText);
                this.Invoke(d, new Object[] { i });
            }
            else
            {
                if (string.IsNullOrEmpty(worker.DataType))
                    this.lbl.Text = "";
                else
                    this.lbl.Text = worker.DataType + ((CustomEventArgs)i).i.ToString();
            }
        }

        private void SetProgress(object i)
        {
            if (this.progressBar1.InvokeRequired)
            {
                SetTextCallback d = new SetTextCallback(SetProgress);
                this.Invoke(d, new Object[] { i });
            }
            else
            {
                if (this.progressBar1.Maximum != worker.DataCountNumber)
                    this.progressBar1.Maximum = worker.DataCountNumber;

                this.progressBar1.Value = ((CustomEventArgs)i).p;
            }
        }

        private delegate void SetTextCallback(CustomEventArgs e);

    }
}
